import tkinter
import tkinter as tk
from tkinter.filedialog import askdirectory, askopenfilename
import os
import struct
# from datetime import datetime
programName = "文件打包程序V1.0"
# pip install pyinstaller
# pyinstaller.exe  -F -w SimPleUI.py -n 文件打包程序V1.0


class Element(object):
    def __init__(self):
        self.path = ""       # 文件的路径
        self.size = int()    # 文件的大小


class SimpleUI(object):
    def __init__(self):
        self.elements = list()
        self.FONT = ('Msyh', 12)
        self.window = tkinter.Tk()
        self.window.title(programName)
        self.notice = tkinter.StringVar()
        self.notice.set("请依次添加：ota.bin, font.bin, logo.bin, ui.bin  最终输出文件为: ota_resource.bin")
        self.outputDirVar = tkinter.StringVar()
        self.elementCountVar = tkinter.StringVar()
        self.rightFrame = tkinter.Frame(self.window)
        self.rightFrame.pack(side=tk.LEFT)
        self.outputFrame = tkinter.Frame(self.rightFrame)
        self.outputFrame.pack(anchor=tk.W, pady=8)
        self.noticeFrame = tkinter.Frame(self.rightFrame)
        self.noticeFrame.pack(anchor=tk.W, pady=8)
        self.headerFrame = tkinter.Frame(self.rightFrame)
        self.headerFrame.pack(anchor=tk.W, pady=8, fill=tk.X)

    def select_out(self):
        self.outputDirVar.set(askdirectory())

    def update_elements(self):
        self.elementCountVar.set("已添加：{}个".format(len(self.elements)))

    def add_element(self):
        element = Element()
        element.path = askopenfilename()
        element.size = os.path.getsize(element.path)
        self.elements.append(element)
        self.update_elements()

    def clear_all(self):
        self.elements.clear()
        self.update_elements()

    def make(self):

        # with open(f'{self.outputDirVar.get()}/ota_resource_{datetime.now().strftime("%Y%m%d_%H%M%S")}.bin',
        with open(f'{self.outputDirVar.get()}/ota_resource.bin',
                  'wb') as out_file:
            for element in self.elements:
                out_file.write(struct.pack("@I", element.size))
            for element in self.elements:
                with open(element.path, 'rb') as in_file:
                    out_file.write(in_file.read())

    def run(self):
        (tkinter.Label(self.outputFrame, text="输出目录", font=self.FONT, width=20, anchor=tk.W)
         .pack(side=tk.LEFT))
        (tkinter.Entry(self.outputFrame, textvariable=self.outputDirVar, font=self.FONT, width=80, state=tk.DISABLED)
         .pack(side=tk.LEFT))
        (tkinter.Button(self.outputFrame, text="选择", font=self.FONT, command=self.select_out)
         .pack(side=tk.LEFT, padx=12))
        (tkinter.Entry(self.noticeFrame, textvariable=self.notice, font=self.FONT, width=90, state=tk.DISABLED)
         .pack(side=tk.LEFT))
        (tkinter.Button(self.headerFrame, text="添加元素", font=self.FONT, command=self.add_element)
         .pack(side=tk.LEFT))
        (tkinter.Entry(self.headerFrame, textvariable=self.elementCountVar, width=20, font=self.FONT, state=tk.DISABLED)
         .pack(side=tk.LEFT, padx=8))
        (tkinter.Button(self.headerFrame, text="生成目标文件", font=self.FONT, command=self.make)
         .pack(side=tk.RIGHT, pady=8))
        (tkinter.Button(self.headerFrame, text="清除", font=self.FONT, command=self.clear_all)
         .pack(side=tk.RIGHT, padx=8))
        self.window.mainloop()


if __name__ == "__main__":
    ui = SimpleUI()
    ui.run()
